package com.itheima.web;

import com.itheima.pojo.*;
import com.itheima.service.BrowseGoodsRecordService;
import com.itheima.service.ItemService;
import com.itheima.service.PurchaseHistoryService;
import com.itheima.service.ShoppingCartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@WebServlet("/salesPredictionServlet")
public class SalesPredictionServlet extends HttpServlet {
    private BrowseGoodsRecordService browseGoodsRecordService = new BrowseGoodsRecordService();
    private ShoppingCartService shoppingCartService = new ShoppingCartService();
    private ItemService itemService = new ItemService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过session获取user对象
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer sellerId = user.getId();

        List<Item> items = itemService.selectBySellerId(sellerId);

        List<Integer> itemIds = new ArrayList<>();
        List<String> itemNames = new ArrayList<>();
        HashMap<String, Integer> itemNameItemIdDict = new HashMap<>();
        for (Item item : items) {
            itemIds.add(item.getId());
            itemNames.add(item.getItemName());
            itemNameItemIdDict.put(item.getItemName(), item.getId());
        }

        // 用于记录每个itemId的预测销量
        HashMap<Integer, Integer> recommendationDict = new HashMap<>();
        // 初始化recommendationDict,设置预测销量为0
        for (Integer itemId : itemIds) {
            recommendationDict.put(itemId, 0);
        }

        for (Integer itemId : itemIds) {
            Integer totalQuantity = shoppingCartService.selectByItemId(itemId);
            if (totalQuantity == null) {
                totalQuantity = 0;
            }
            Integer value = recommendationDict.get(itemId) + totalQuantity;
            recommendationDict.put(itemId, value);
        }

        for (String itemName : itemNames) {
            Integer totalQuantity = browseGoodsRecordService.selectByItemName(itemName);
            if (totalQuantity == null) {
                totalQuantity = 0;
            }
            Integer itemId = itemNameItemIdDict.get(itemName);
            Integer value = recommendationDict.get(itemId) + totalQuantity;
            recommendationDict.put(itemId, value);
        }

        for (Item item : items) {
            item.setSalesPrediction(recommendationDict.get(item.getId()));
        }

        request.setAttribute("items", items);

        request.getRequestDispatcher("/sales-prediction.jsp").forward(request, response);

//        // 创建一个List对象，用于存储HashMap中的所有键值对
//        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(recommendationDict.entrySet());

//        // 使用Collections.sort()方法对List对象进行排序，排序的依据是HashMap的value值，从大到小排序
//        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
//            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
//                return o2.getValue().compareTo(o1.getValue());
//            }
//        });
//
//        // 创建一个新的List对象，用于存储value最大的三个键
//        List<Integer> maxKeys = new ArrayList<>();
//
//        // 遍历排序后的List对象，并将前三个键存储到新的List对象中
//        int count = 0;
//        for (Map.Entry<Integer, Integer> entry : list) {
//            if (count < 3) {
//                maxKeys.add(entry.getKey());
//                count++;
//            } else {
//                break;
//            }
//        }

//        // 输出结果
//        System.out.println("HashMap: " + recommendationDict);
//        System.out.println("Value最大的三个键: " + maxKeys);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
